home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / dos / prg / pas / swag / oop.swg / 0027_Flexible OOP Array.pas < prev    next >
Pascal/Delphi Source File  |  1993-11-02  |  3KB  |  111 lines

  1. {
  2. From: TODD HOLMES
  3. Heres a flexible OOP array...}
  4.  
  5. { $TESTED}
  6.  
  7. Uses Objects;
  8. Type
  9.  
  10.   TestRec = Record
  11.     Name: String[20];
  12.     Age : Word;
  13.    end;
  14.    {A TestRecord}
  15.  
  16.   PAByte = ^TAByte;
  17.   TAByte = Array[0..65519] of byte;
  18.   {General byte array}
  19.  
  20. {TArray is limited to 65520 bytes of data, and may store any type
  21. of data.}
  22.  
  23.   PArray = ^TArray;
  24.   TArray = Object(TObject)
  25.     Data    : PAByte;
  26.     DataSize: Word;    {Size of the Data to hold}
  27.     MaxCount: Word;    {Maximum amount of items of DataSize}
  28.     Count   : Word;    {How many items in Array}
  29.    Constructor Init(ADataSize,ACount:Word);
  30.    Constructor Load(Var S:TStream);
  31.    Procedure   Store(VAR S:TStream); Virtual;
  32.    Destructor  Done;Virtual;
  33.    Procedure   GetItem(Index:Word;Var Item);
  34.    Procedure   PutItem(Index:Word;Var Item);
  35.  end;
  36.  
  37. Constructor TArray.Init(ADataSize,ACount:Word);
  38.  begin
  39.   Inherited Init;  {TP6 Tobject.init}
  40.   DataSize := ADataSize;
  41.   MaxCount := 65520 div ADataSize;   {For Error Checking}
  42.   If Acount > MaxCount then Fail;    {Array is too big}
  43.   Count    := ACount;
  44.   GetMem(Data,Count * DataSize);     {Get Mem for the array}
  45.   FillChar(Data^,Count * DataSize,0);{Clear the Array}
  46.  end;
  47.  
  48. Constructor TArray.Load(Var S:TStream);
  49.  begin
  50.   With S do begin
  51.    Read(DataSize,SizeOf(DataSize));
  52.    Read(MaxCount,SizeOf(MaxCount));
  53.    Read(Count,SizeOf(MaxCount));
  54.    GetMem(Data,Count * DataSize);
  55.    Read(Data^,Count * DataSize);
  56.   end;
  57.  end;
  58.  
  59. Procedure TArray.Store(Var S:TStream);
  60.  begin
  61.   With S do Begin
  62.    Write(DataSize,SizeOf(DataSize));
  63.    Write(MaxCount,SizeOf(MaxCount));
  64.    Write(Count,sizeOf(Count));
  65.    Write(Data^,Count * DataSize);
  66.   end;
  67.  end;
  68.  
  69. Destructor TArray.done;
  70.  begin
  71.   FreeMem(Data,Count*DataSize);
  72.   Inherited Done;
  73.  end;
  74.  
  75. Procedure TArray.GetItem(Index:Word;Var Item);
  76.  begin
  77.   If Index > count then Exit;
  78.   Move(Data^[(Index - 1) * DataSize],Item,DataSize);
  79.  end;
  80.  
  81. Procedure TArray.PutItem(Index:Word;Var Item);
  82.  begin
  83.  If Index > count then exit;
  84.   Move(Item,Data^[(Index - 1) * DataSize],DataSize);
  85.  end;
  86.  
  87. Var
  88.    Flexable:PArray;
  89.    TR:TestRec;
  90.     I:Integer;
  91.  
  92. begin
  93.  Randomize;
  94.  Flexable := New(PArray,Init(SizeOf(TR),10));
  95.  If Flexable <> Nil then begin; {Array to big}
  96.    For I := 1 to Flexable^.Count do begin
  97.      With TR do begin
  98.       Name := 'Bobby Sue';
  99.       Age  := I;
  100.      end;
  101.      Flexable^.PutItem(I,TR);
  102.     end;
  103.    For I := 1 to FlexAble^.Count do begin
  104.      FlexAble^.GetItem(I,TR);
  105.      With Tr do
  106.        Writeln('Rec ',I:2,' is Name: ',Name:20,' Age: ',Age:8);
  107.     end;
  108.    end;
  109.  Dispose(Flexable,Done);
  110. end.
  111.